package com.onlineMIS.ORM.DAO.headQ.supplier.finance;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.onlineMIS.ORM.DAO.BaseDAO;
import com.onlineMIS.ORM.DAO.headQ.finance.FinanceCategoryImpl;
import com.onlineMIS.ORM.entity.base.BaseOrder;
import com.onlineMIS.ORM.entity.chainS.user.ChainStore;
import com.onlineMIS.ORM.entity.headQ.finance.FinanceBill;
import com.onlineMIS.ORM.entity.headQ.finance.FinanceBillItem;
import com.onlineMIS.ORM.entity.headQ.finance.FinanceCategory;
import com.onlineMIS.ORM.entity.headQ.finance.FinanceCategoryFlow;
import com.onlineMIS.ORM.entity.headQ.supplier.finance.FinanceBillSupplier;
import com.onlineMIS.ORM.entity.headQ.supplier.finance.FinanceBillSupplierItem;

@Repository
public class FinanceCategoryFlowDaoImpl  extends BaseDAO<FinanceCategoryFlow>  {
	
	@Autowired
	private FinanceCategoryImpl financeCategoryImpl;

    /**
     * 更新财务账目流水
     * @param fSupplier 供应商
     * @param financeBill 客户
     * @param isCancel 是否红冲
     * @param isDelete 是否删除
     */
	public void updateFinanceCategoryFlow(FinanceBillSupplier fSupplier, FinanceBill bill, Boolean isCancel,Boolean isDelete) {
		int offset = 1;
		int billType = 0;

		offset *= isCancel ? -1 : 1;
			
		if (fSupplier != null) {
			billType = fSupplier.getType();
			
			if (billType == FinanceBillSupplier.FINANCE_INCOME_HQ || billType == FinanceBillSupplier.FINANCE_INCREASE_HQ) {
				offset *= 1;
			}else if (billType == FinanceBillSupplier.FINANCE_PAID_HQ || billType == FinanceBillSupplier.FINANCE_DECREASE_HQ) {
				offset *= -1;
			}
			
			// 1. to update the bill items
			for (FinanceBillSupplierItem billItem : fSupplier.getFinanceBillItemSet()) {
				int categoryId = billItem.getFinanceCategory().getId();
				double amount = billItem.getTotal();
				if (amount != 0.0) {
					FinanceCategoryFlow flow = new FinanceCategoryFlow();
					flow.setFinanceCategoryId(categoryId);
					flow.setAmt(offset * amount);
					String isDrop = isCancel ? "C:" : "O:";  // 红冲还是正常入账
					String isSupplice = fSupplier != null ? "S" : "C";  // 客户or供应商
					flow.setComment(isDrop + isSupplice + ":" + fSupplier.getId());
					flow.setDate(fSupplier.getBillDate());
					this.save(flow, true);
				}
			}
		}else {
			billType = bill.getType();
			
			if (billType == FinanceBill.FINANCE_INCOME_HQ || billType == FinanceBill.FINANCE_INCREASE_HQ || billType == FinanceBill.FINANCE_PREINCOME_HQ) {
				offset *= 1;
			}else if (billType == FinanceBill.FINANCE_PAID_HQ || billType == FinanceBill.FINANCE_DECREASE_HQ || billType == FinanceBill.FINANCE_PREINCOME_SPEND_HQ) {
				offset *= -1;
			}
			
			// 1. to update the bill items
			for (FinanceBillItem billItem : bill.getFinanceBillItemSet()) {
				int categoryId = billItem.getFinanceCategory().getId();
				
				double amount = billItem.getTotal();
				if (amount != 0.0) {
					FinanceCategoryFlow flow = new FinanceCategoryFlow();
					flow.setFinanceCategoryId(categoryId);
					flow.setAmt(offset * amount);
					String isSupplice = fSupplier != null ? "S" : "C";
					String isDrop = isCancel ? "C:" : "O:";  // 红冲还是正常入账
					flow.setComment(isDrop + isSupplice + ":" + bill.getId());
					flow.setDate(bill.getBillDate());
					this.save(flow, true);
				}
			}
			
			
			//如果是预存金单据，补充预存金增加数据一条
			if (billType == FinanceBill.FINANCE_PREINCOME_HQ) {
				FinanceCategory prepaidCategory = financeCategoryImpl.getByTypeId(FinanceCategory.PREPAY_ACCT_TYPE);
				
				FinanceCategoryFlow flow = new FinanceCategoryFlow();
				flow.setFinanceCategoryId(prepaidCategory.getId());
				flow.setAmt(offset * bill.getInvoiceTotal());
				String isSupplice = fSupplier != null ? "S" : "C";
				String isDrop = isCancel ? "C:" : "O:";  // 红冲还是正常入账
				flow.setComment(isDrop + isSupplice + ":" + bill.getId());
				flow.setDate(bill.getBillDate());
				this.save(flow, true);
			}
			
		}
	}

	/**
	 * 获取该卡在startTime之前的流水sum
	 * @param financeCategoryId
	 * @param startDate
	 * @return
	 */
	public double selectSumAmt(int financeCategoryId, Date startDate) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceCategoryFlow.class);
		criteria.setProjection(Projections.sum("amt"));
		criteria.add(Restrictions.eq("financeCategoryId", financeCategoryId));
		criteria.add(Restrictions.lt("date", startDate));
		//未软删的流水
		criteria.add(Restrictions.eq("delflag", FinanceCategoryFlow.IS_NORMAL));
		List<Object> amt =  this.getByCriteriaProjection(criteria, true);
		if (amt.size() !=0 && amt.get(0) != null) {
			return (double) amt.get(0);
		}else {
			return 0d;
		}		
	}
	
}
